#!/usr/local/bin/python
# -*- coding: utf-8 -*-


import calendar
import json
from datetime import datetime
from dateutil.relativedelta import relativedelta
from xToolkit import xstring

from data_alchemy.app_config_alchemy import AppConfigAlchemy
from initLogger import logger

def splitVoucher(voucherStr):
    '''
    函数功能：对科目字符串进行拆分
    :param voucherStr:
    :return:
    '''



    voucherItems = voucherStr.split("->")
    for i in range(len(voucherItems), 6):
        voucherItems.append("")

    return voucherItems

def getVoucherTail(voucherStr):
    '''
    函数功能：获取科目尾巴
    :return:
    '''

    voucherItems = voucherStr.split("->")
    length = len(voucherItems)

    return voucherItems[length-1]




def monthlyChange(year, month):
    '''
    函数功能：将月份信息转换为起始日期
    :param year:
    :param month:
    :return:
        startDateStr  开始日期
        endDateStr  结束日期
    '''

    myDatetime = "%d-%d-01" % (year, month)
    startDate = datetime.strptime(myDatetime, '%Y-%m-%d')
    startDateStr = startDate.strftime("%Y-%m-%d")

    myDatetime = "%d-%d-%d" % (year, month, calendar.monthrange(year, month)[1])
    endDate = datetime.strptime(myDatetime, '%Y-%m-%d')
    endDateStr = endDate.strftime("%Y-%m-%d")

    return startDateStr, endDateStr


#https://blog.csdn.net/ol_m_lo/article/details/120322460
def getLastMonth(originalDatetime, number=1):
    '''
    函数功能：获取前几个月
    :param originalDatetime:
    :param number:
    :return:
    '''
    month_date = originalDatetime - relativedelta(months=number)
    return month_date

def getLastMonthInt(originalYear, originalMonth, number=1):
    '''
    函数功能：计算上个月的月份和年份
    :param originalYear:
    :param originalMonth:
    :return:
    '''
    originalDatetime = "%d-%d-01" % (originalYear, originalMonth)
    originalDate = datetime.strptime(originalDatetime, '%Y-%m-%d')

    newDatetime = getLastMonth(originalDatetime=originalDate, number=number)

    newYear = newDatetime.year
    newMonth = newDatetime.month
    return newYear, newMonth

def getLastYear(originalDatetime, number=1):
    '''
    函数功能：获取前几个月
    :param originalDatetime:
    :param number:
    :return:
    '''
    newDate = originalDatetime - relativedelta(years=number)
    return newDate

def setTargetDate(year, month):
    '''
    函数功能；按照年份、月份设置目标日期
    :param year:
    :param month:
    :return:
    '''

    originalDatetime = "%d-%d-01" % (year, month)
    targetDate = datetime.strptime(originalDatetime, '%Y-%m-%d')

    return targetDate
def setLastDayOfMonth(year, month):
    '''
    函数功能：获得指定月份最后一天的日期
    :param year:
    :param month:
    :return:
    '''

    # 获取当前月份的最后一天
    last_day_of_month = calendar.monthrange(year, month)[1]
    last_day = datetime(year, month, last_day_of_month)
    return last_day

def getNextMonth(originalDatetime, number=1):
    '''
    函数功能：获取后几个月
    :param originalDatetime:
    :param number:
    :return:
    '''

    month_date = originalDatetime + relativedelta(months=number)
    return month_date

def getNextYear(originalDatetime, number=1):
    '''
    函数功能：获取后几个月
    :param originalDatetime:
    :param number:
    :return:
    '''

    newDate = originalDatetime + relativedelta(years=number)
    return newDate

def genFullSubjectList(subjectJsonTree):
    '''
    函数功能：生成全面的科目列表，将树形结构的数据变成列表形式
    :return:
    '''
    treeContextInfo = json.loads(subjectJsonTree)
    subjectNameElementList=[]
    subjectList=[]

    def parseVoucherTree(curTreeInfo):
        for key in  curTreeInfo.keys():
            if curTreeInfo[key] != "":
                #还有子节点
                subjectNameElementList.append(key)
                parseVoucherTree(curTreeInfo[key])
                listLen = len(subjectNameElementList)
                del subjectNameElementList[listLen - 1]
            else:
                subjectNameStr =""
                for subjectNameItem in subjectNameElementList:
                    subjectNameStr = subjectNameStr + subjectNameItem +"->"

                subjectNameStr = subjectNameStr + key
                subjectList.append(subjectNameStr)





    for key in treeContextInfo.keys():
        subjectNameElementList.append(key)
        parseVoucherTree(treeContextInfo[key])
        listLen = len(subjectNameElementList)
        del subjectNameElementList[listLen-1]



    for subjectListItem in subjectList:
        logger.debug(subjectListItem)

    return subjectList




def processValueSignificant(sourceValue):
    '''
    函数功能：当数据过小时，xstring.dispose(dataRow.start_value).humanized_amount(complex=True)
    处理数据会出错，为了解决这个问题。对数据做一个特殊处理。
    :param sourceValue:
    :return:
    b2 = float('%.9f' % a1)
    effective digit   processValueSignificant
    两位有效数字
    '''

    sourceValue = float('%.2f' % sourceValue)
    if sourceValue<0.0001 and sourceValue>-0.0001:
        return 0.0
    else:
        return sourceValue


def specialMinValue(valueList):
    '''
    函数功能：获取出0以外的，列表中最小是数值
    :return:
    '''
    minValue=0.0
    for curValue in valueList:
        #如果当前值是0，不做处理
        if curValue==0:
            continue

        #如果最小值是0，将当前值设置为最小值
        if minValue==0:
            minValue=curValue
        else:
            #如果当前值比最小值小，把当前值设置我最小值
            if curValue<minValue:
                minValue=curValue
    return minValue


def fromVoucherPathToName(VoucherPath):
    '''
    函数功能：从记账凭证路径中提取记账凭证名称
    :return:
    '''
    subStringList = VoucherPath.split("->")
    substringListLen = len(subStringList)
    voucherName = subStringList[substringListLen - 1]
    return  voucherName



def main():
    a=711.9999999999997
    a1 = 711.222222222

    setLastDayOfMonth(2024, 1)

    endValueStr = xstring.dispose(a).humanized_amount(complex=True)
    b = float('%.9g' % a)
    b1 = float('%.9g' % a1)
    b2 = float('%.9f' % a1)
    c = round(a,3)
    c1 = round(a1, 3)
    endValueStr1 = xstring.dispose(b).humanized_amount(complex=True)

    print(1)
    pass
    '''
    result = splitVoucher("资产->自有->货币->银行存款->交通银行")

    logger.debug(result)


    property_increase_value = 2.53423523
    propertyIncreaseValueStr = "%.2f" % (property_increase_value)
    logger.debug(propertyIncreaseValueStr)

    property_increase_value = 2
    propertyIncreaseValueStr = "%.2f" % (property_increase_value)
    logger.debug(propertyIncreaseValueStr)

    property_increase_value = 0
    propertyIncreaseValueStr = "%.2f" % (property_increase_value)
    logger.debug(propertyIncreaseValueStr)

    startDateStr, endDateStr = monthlyChange(2000, 2)
    logger.debug(startDateStr)
    logger.debug(endDateStr)
    '''

if __name__ == '__main__':
    main()